package com.hnucm.c201901020121.Activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.hnucm.c201901020121.R;
import com.hnucm.c201901020121.Utils.Bmob.BUser;

import java.util.List;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.datatype.BmobQueryResult;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SQLQueryListener;
import cn.bmob.v3.listener.SaveListener;

public class LoginAct extends AppCompatActivity {
    Button loginBt;
    TextView loginTv, registerTv;
    EditText tel,pas;
    public static BUser currentUser;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Bmob.initialize(this, "7a61c2b6351f923a4db4882d696a5767");
        loginBt=findViewById(R.id.button6);
        loginTv=findViewById(R.id.textView41);
         registerTv=findViewById(R.id.textView42);
        tel=findViewById(R.id.editTextTextPersonName2);
        pas=findViewById(R.id.editTextTextPersonName3);
        loginTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loginTv.setBackground(getResources().getDrawable(R.drawable.login_button_bg));
                loginTv.setTextColor(Color.parseColor("#F87210"));
                 registerTv.setBackground(getResources().getDrawable(R.drawable.register_button_bg));
                 registerTv.setTextColor(Color.parseColor("#BBB9B5"));
                loginBt.setText("立即登录");
            }
        });
         registerTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 registerTv.setBackground(getResources().getDrawable(R.drawable.login_button_bg));
                 registerTv.setTextColor(Color.parseColor("#F87210"));
                loginTv.setBackground(getResources().getDrawable(R.drawable.register_button_bg));
                loginTv.setTextColor(Color.parseColor("#BBB9B5"));
                loginBt.setText("立即注册");
            }
        });
        loginBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(tel.getText().toString()==null){
                    Toast.makeText(LoginAct.this,"请输入用户名", Toast.LENGTH_LONG);
                }
                else if(pas.getText().toString()==null){
                    Toast.makeText(LoginAct.this,"请输入密码", Toast.LENGTH_LONG);
                }
                else{
                    judgeData();
                }
            }
        });
    }
    void judgeData(){
        if(loginBt.getText().toString().equals("立即注册")){
            register();
            return;
        }
        String userName,password;
        userName=tel.getText().toString();
        password=pas.getText().toString();
        String sql = "select * from BUser where userName='"+userName+"'";
        Log.i("bmob",sql);
        BmobQuery<BUser> query = new BmobQuery<BUser>();
        query.setSQL(sql);
        query.doSQLQuery(new SQLQueryListener<BUser>() {
            @RequiresApi(api = Build.VERSION_CODES.N)
            @Override
            public void done(BmobQueryResult<BUser> bmobQueryResult, BmobException e) {
                if (e == null) {
                    List<BUser> list = (List<BUser>) bmobQueryResult.getResults();
                    if(list.size()==0){
                        Toast.makeText(LoginAct.this,"用户名输入错误",Toast.LENGTH_SHORT);
                    }
                    else if(list.get(0).getPassword().equals(password)){
                        currentUser=list.get(0);
                        Toast.makeText(LoginAct.this,"登录成功",Toast.LENGTH_SHORT);
                        Intent intent=new Intent(LoginAct.this,MainActivity.class);
                        startActivity(intent);
                    }
                    else{
                        Toast.makeText(LoginAct.this,"输入密码错误，请重新输入！",Toast.LENGTH_SHORT);
                    }
                } else {
                        Toast.makeText(LoginAct.this,"用户名输入错误",Toast.LENGTH_SHORT);
                }
            }
        });
    }
    private void register(){

        BUser bUser=new BUser();
        bUser.setPassword(pas.getText().toString());
        bUser.setUserName(tel.getText().toString());
        bUser.setHeadimg("http://photogz.photo.store.qq.com/psc?/V13FgbWj3x2RyI/05RlWl8gsTOH*Z17MtCBzGVJKkBOpV7XGo3O6cA00xROqS8Faapau4opR99sRdrlRrDWi3vUFCGWmZKNtIF4lg!!/m&bo=gACAAIAAgAARADc!");
        bUser.setAmount("0");
        bUser.setCriditCore(100);
        bUser.setSumIncome("0");
        bUser.save(new SaveListener<String>() {
            @Override
            public void done(String s, BmobException e) {
                if (e == null) {
                    currentUser=bUser;
                    Toast.makeText(LoginAct.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(LoginAct.this, MainActivity.class);
                    startActivity(intent);
                } else {
                    Log.i("EditTaskAck",e.toString());
                }
            }
        });
    }
}